<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
 <script language="javascript">
    var d,d1;

  //获取开始时间
  function showTime(){
     d = new Date();                               
     var s = "";
                     
     s += (d.getMonth() + 1)   +   "-";            
     s += d.getDate() + " ";                       
     s += d.getHours() + ":";                      
     s += d.getMinutes() + ":";                     
     s += d.getSeconds() + ":";                    
     s += d.getMilliseconds();                      
  
     document.getElementById("starttime").value = s;    
     document.getElementById("endtime").value = "";     
     document.getElementById("interval").value = "";    
  }

  //获取结束时间
  function showTime2(){
     d1 = new Date();   
     var s1 = "";
      
     s1 += (d1.getMonth() + 1) + "-";  
     s1 += d1.getDate() + " "; 
     s1 += d1.getHours() + ":";
     s1 += d1.getMinutes() + ":";
     s1 += d1.getSeconds() + ":";
     s1 += d1.getMilliseconds();
 
     document.getElementById("endtime").value = s1;
     document.getElementById("interval").value = ""; 
  }

  //计算时间差
  function showInterval(){
  
   var sjc = ((d1.getTime()-d.getTime())/1000);
     if(sjc <= 0){
       alert("结束时间不能比开始时间早！"); 
    }else{
       document.getElementById("interval").value = sjc + "秒";   
    }
  }
 </script>
 </head>
 
 <body><div>
  <p>
    <input type="button" value="开始时间" onclick="showTime()" />
    <input type="text" id="starttime" /><br>
    
    <input type="button" value="结束时间" onclick="showTime2()" />
    <input type="text" id="endtime" /><br>
    
    <input type="button" value="时间差" onclick="showInterval()"/>
    <input type="text" id="interval" />
  </p>
  <p>&nbsp;</p></div>
 </body>
 
<html>

